home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / clang / dosrcss.zip / MAKETIME.C < prev    next >
C/C++ Source or Header  |  1990-07-18  |  7KB  |  247 lines

  1. #
  2. /*
  3.  * MAKETIME        derive 32-bit time value from TM structure.
  4.  *
  5.  * Usage:
  6.  *    long t,maketime();
  7.  *    struct tm *tp;    Pointer to TM structure from <time.h>
  8.  *            NOTE: this must be extended version!!!
  9.  *    t = maketime(tp);
  10.  *
  11.  * Returns:
  12.  *    0 if failure; parameter out of range or nonsensical.
  13.  *    else long time-value.
  14.  * Notes:
  15.  *    This code is quasi-public; it may be used freely in like software.
  16.  *    It is not to be sold, nor used in licensed software without
  17.  *    permission of the author.
  18.  *    For everyone's benefit, please report bugs and improvements!
  19.  *     Copyright 1981 by Ken Harrenstien, SRI International.
  20.  *    (ARPANET: KLH @ SRI)
  21.  */
  22. #ifndef lint
  23. static char rcsid[]= "$Id: maketime.c,v 5.2 90/07/15 11:31:54 ROOT_DOS Release $";
  24. #endif
  25. /* $Log:    maketime.c,v $
  26.  * Revision 5.2  90/07/15  11:31:54  ROOT_DOS
  27.  * DOS version of RCS 4.0 checked in for MODS
  28.  * by lfk@athena.mit.edu
  29.  * Also update to MSC 6.0
  30.  * 
  31.  * Revision 1.8  88/11/08  13:54:53  narten
  32.  * allow negative timezones (-24h <= x <= 24h)
  33.  * 
  34.  * Revision 1.7  88/11/08  12:02:24  narten
  35.  * changes from  eggert@sm.unisys.com (Paul Eggert)
  36.  * 
  37.  * Revision 1.7  88/08/28  14:47:52  eggert
  38.  * Allow cc -R.  Remove unportable "#endif XXX"s.
  39.  * 
  40.  * Revision 1.6  87/12/18  17:05:58  narten
  41.  * include rcsparam.h
  42.  * 
  43.  * Revision 1.5  87/12/18  11:35:51  narten
  44.  * maketime.c: fixed USG code - you have tgo call "tzset" in order to have
  45.  * "timezone" set. ("localtime" calls it, but it's probably better not to 
  46.  * count on "localtime" having been called.)
  47.  * 
  48.  * Revision 1.4  87/10/18  10:26:57  narten
  49.  * Updating version numbers. Changes relative to 1.0 are actually 
  50.  * relative to 1.2
  51.  * 
  52.  * Revision 1.3  87/09/24  13:58:45  narten
  53.  * Sources now pass through lint (if you ignore printf/sprintf/fprintf 
  54.  * warnings)
  55.  * 
  56.  * Revision 1.2  87/03/27  14:21:48  jenkins
  57.  * Port to suns
  58.  * 
  59.  * Revision 1.1  84/01/23  14:50:04  kcs
  60.  * Initial revision
  61.  * 
  62.  * Revision 1.2  83/12/05  10:12:56  wft
  63.  * added cond. compilation for USG Unix; long timezone;
  64.  * 
  65.  * Revision 1.1  82/05/06  11:38:00  wft
  66.  * Initial revision
  67.  * 
  68.  */
  69.  
  70.  
  71. #include "rcsbase.h"
  72. #include "time.h"
  73.  
  74. int daytb[] = {   /* # days in year thus far, indexed by month (0-12!!) */
  75.     0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365
  76. };
  77.  
  78. struct tm *localtime();
  79. long    time();
  80.  
  81. long maketime(atm)
  82. struct tm *atm;
  83. {    register struct tm *tp;
  84.     register int i;
  85.     int year, yday, mon, day, hour, min, sec, zone, dst, leap;
  86.     long tres, curtim;
  87.  
  88.     VOID time(&curtim);
  89.     tp = localtime(&curtim);        /* Get breakdowns of current time */
  90.     year = tp->tm_year;        /* Use to set up defaults */
  91.     mon = tp->tm_mon;
  92.     day = tp->tm_mday;
  93.  
  94.  
  95. #ifdef DEBUG
  96. printf("first YMD: %d %d %d, T=%ld\n",year,mon,day,tres);
  97. #endif
  98.     tp = atm;
  99.  
  100.     /* First must find date, using specified year, month, day.
  101.      * If one of these is unspecified, it defaults either to the
  102.      * current date (if no more global spec was given) or to the
  103.      * zero-value for that spec (i.e. a more global spec was seen).
  104.      * Start with year... note 32 bits can only handle 135 years.
  105.      */
  106.     if(tp->tm_year != TMNULL)
  107.       {    if((year = tp->tm_year) >= 1900)    /* Allow full yr # */
  108.               year -= 1900;            /* by making kosher */
  109.         mon = 0;        /* Since year was given, default */
  110.         day = 1;        /* for remaining specs is zero */
  111.       }
  112.     if(year < 70 || 70+134 < year )    /* Check range */
  113.         return(0);        /* ERR: year out of range */
  114.     leap = year&03 ? 0 : 1;        /* See if leap year */
  115.     year -= 70;            /* UNIX time starts at 1970 */
  116.  
  117.     /*
  118.      * Find day of year.
  119.      * YDAY is used only if it exists and either the month or day-of-month
  120.      * is missing.
  121.      */
  122.     if (tp->tm_yday != TMNULL
  123.      && (tp->tm_mon == TMNULL || tp->tm_mday == TMNULL))
  124.         yday = tp->tm_yday;
  125.     else
  126.       {    if(tp->tm_mon  != TMNULL)
  127.           {    mon = tp->tm_mon;    /* Month was specified */
  128.             day = 1;        /* so set remaining default */
  129.           }
  130.         if(mon < 0 || 11 < mon) return(0);    /* ERR: bad month */
  131.         if(tp->tm_mday != TMNULL) day = tp->tm_mday;
  132.         if(day < 1
  133.          || (((daytb[mon+1]-daytb[mon]) < day)
  134.             && (day!=29 || mon!=1 || !leap) ))
  135.                 return(0);        /* ERR: bad day */
  136.         yday = daytb[mon]    /* Add # of days in months so far */
  137.           + ((leap        /* Leap year, and past Feb?  If */
  138.               && mon>1)? 1:0)    /* so, add leap day for this year */
  139.           + day-1;        /* And finally add # days this mon */
  140.  
  141.                 if (tp->tm_yday != TMNULL       /* Confirm that YDAY correct */
  142.                  && tp->tm_yday != yday) return(0);     /* ERR: conflict */
  143.       }
  144.     if(yday < 0 || (leap?366:365) <= yday)
  145.         return(0);        /* ERR: bad YDAY or maketime bug */
  146.  
  147.     tres = year*365            /* Get # days of years so far */
  148.         + ((year+1)>>2)        /* plus # of leap days since 1970 */
  149.         + yday;            /* and finally add # days this year */
  150.  
  151.         if((i = tp->tm_wday) != TMNULL) /* Check WDAY if present */
  152.                 if(i < 0 || 6 < i       /* Ensure within range */
  153.                   || i != (tres+4)%7)   /* Matches? Jan 1,1970 was Thu = 4 */
  154.                         return(0);      /* ERR: bad WDAY */
  155.  
  156. #ifdef DEBUG
  157. printf("YMD: %d %d %d, T=%ld\n",year,mon,day,tres);
  158. #endif
  159.     /*
  160.      * Now determine time.  If not given, default to zeros
  161.      * (since time is always the least global spec)
  162.      */
  163.     tres *= 86400L;            /* Get # seconds (24*60*60) */
  164.     hour = min = sec = 0;
  165.     if(tp->tm_hour != TMNULL) hour = tp->tm_hour;
  166.     if(tp->tm_min  != TMNULL) min  = tp->tm_min;
  167.     if(tp->tm_sec  != TMNULL) sec  = tp->tm_sec;
  168.     if( min < 0 || 60 <= min
  169.      || sec < 0 || 60 <= sec) return(0);    /* ERR: MS out of range */
  170.     if(hour < 0 || 24 <= hour)
  171.         if(hour != 24 || (min+sec) !=0)    /* Allow 24:00 */
  172.             return(0);        /* ERR: H out of range */
  173.  
  174.     /* confirm AM/PM if there */
  175.     switch(tp->tm_ampm)
  176.       {    case 0: case TMNULL:    /* Ignore these values */
  177.             break;
  178.         case 1:            /* AM */
  179.         case 2:            /* PM */
  180.             if(hour > 12) return(0);  /* ERR: hrs 13-23 bad */
  181.             if(hour ==12) hour = 0;    /* Modulo 12 */
  182.             if(tp->tm_ampm == 2)    /* If PM, then */
  183.                 hour += 12;    /*   get 24-hour time */
  184.             break;
  185.         default: return(0);    /* ERR: illegal TM_AMPM value */
  186.       }
  187.  
  188.     tres += sec + 60L*(min + 60L*hour);    /* Add in # secs of time */
  189.  
  190. #ifdef DEBUG
  191. printf("HMS: %d %d %d T=%ld\n",hour,min,sec,tres);
  192. #endif
  193.     /*
  194.      * We now have the GMT date/time and must make final
  195.      * adjustment for the specified time zone.  If none is specified,
  196.      * the local time-zone is assumed.
  197.      */
  198.     if((zone = tp->tm_zon) == TMNULL    /* If unspecified */
  199.      || (zone == 1))            /* or local-zone requested */
  200.         zone = localzone();        /* then set to local zone */
  201.     if(zone < -24*60 || 24*60 <= zone)
  202.         return(0);            /* ERR: zone out of range */
  203.  
  204.     /* See if must apply Daylight Saving Time shift.
  205.      * Note that if DST is specified, validity is not checked.
  206.      */
  207.     if((dst = tp->tm_isdst) == TMNULL)    /* Must we figure it out? */
  208.       {    curtim = tres +localzone()*60L;    /* Yuck.  Get equiv local */
  209.         dst = localtime(&curtim)->tm_isdst;     /* time, and ask. */
  210.       }
  211.     tres += zone*60L -(dst?3600:0);    /* Add in # seconds of zone adj */
  212.  
  213.     return(tres);
  214. }
  215.  
  216.  
  217. /* LOCALZONE        return local timezone in # mins west of GMT
  218.  *
  219.  */
  220.  
  221. #if defined(V6) || defined(USG)
  222. extern long timezone;
  223. #else
  224. #include <sys/types.h>
  225. #include <sys/timeb.h>
  226. #endif
  227.  
  228. static int lclzon;
  229. localzone()
  230. {
  231.     if (!lclzon) {
  232. #if defined(V6) || defined(USG)
  233. #ifdef USG
  234.     tzset();
  235. #endif
  236.     lclzon = timezone/60 + 1;
  237. #else
  238.     struct timeb tb;
  239.  
  240.     ftime(&tb);
  241.     lclzon = tb.timezone + 1;
  242.  
  243. #endif
  244.     }
  245.     return lclzon - 1;
  246. }
  247.